<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="toolsBreakdownRef"
      :columns="columns"
      :onRowSelectChange="selectChange"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
      :isExportExcel="true"
      selectionType="single"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="showNewBreakOrder">新增</el-button>
        <el-button size="small" type="primary" @click="deleteBreakOrder">作废</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="newBreakOrderShow" title="工具报损新增" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <newBreakOrder ref="newBreakOrder" @close="close" />
    </BaseDialog>
    <BaseDialog :visible.sync="editLossOrderShow" title="工具报损编辑" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <editBreakOrder ref="editBreakOrder" @close="close" :rowData="orderNo" />
    </BaseDialog>
    <BaseDialog :visible.sync="lossOrderShow" title="工具报损详情" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <showBreakOrder ref="showBreakOrder" @close="close" :rowData="orderNo" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import newBreakOrder from './newBreakOrder';
import editBreakOrder from './editBreakOrder';
import showBreakOrder from './showBreakOrder';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { confirmAction, notifyAction } from '@/utils';
import { queryToolsLoss, deleteLossOrder } from '@/api/part/tools';

export default {
  mounted() {
    this.getAllUser();
  },
  mixins: [dictionary],
  components: {
    newBreakOrder,
    showBreakOrder,
    editBreakOrder
  },
  data() {
    return {
      newBreakOrderShow: false,
      orderNo: '',
      editLossOrderShow: false,
      lossOrderShow: false,
      selectList: [],
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: queryToolsLoss,
      fetchParams: { xhrAbort: true },
      showPanel: false,
      syncTableData: [] // 同步的table数据
    };
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '报损单号',
          fieldName: 'lossNo'
        },
        {
          type: 'SELECT',
          label: '报损类型',
          fieldName: 'orderType',
          itemList: [
            { text: '借用报损', value: '82491030' },
            { text: '库存报损', value: '82491031' }
          ]
        },
        {
          type: 'INPUT',
          label: '借用单号',
          fieldName: 'borrowNo'
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remarks'
        }
      ];
    },
    showNewBreakOrder() {
      this.newBreakOrderShow = true;
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={props.row.status == 10041001} onClick={() => this.editOrder(props.row)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.showOrder(props.row)}>
                  详情
                </el-button>
              </div>
            );
          }
        },
        {
          title: '报损单号',
          dataIndex: 'lossNo',
          sorter: true
        },
        {
          title: '报损类型',
          dataIndex: 'orderType',
          filterType: 'checkbox',
          dictItems: this.createDictList('8249'),
          sorter: true
        },
        {
          title: '借用单号',
          dataIndex: 'borrowNo',
          sorter: true
        },
        {
          title: '是否出库',
          dataIndex: 'status',
          filterType: 'checkbox',
          dictItems: this.createDictList('1004'),
          sorter: true
        },
        {
          title: '报损数量',
          dataIndex: 'counts',
          filterType: 'number',
          sorter: true
        },
        {
          title: '报损日期',
          filterType: 'input',
          dataIndex: 'lossDate',
          valueFormat: 'yyyy-MM-dd',
          sorter: true
        },
        {
          title: '经手人',
          dataIndex: 'lossUser',
          sorter: true,
          filterType: 'checkbox',
          dictItems: []
        },
        {
          title: '备注',
          dataIndex: 'remarks',
          sorter: true
        }
      ];
    },
    close() {
      this.newBreakOrderShow = false;
      this.editLossOrderShow = false;
      this.lossOrderShow = false;
      this.fetchParams = Object.assign(this.fetchParams, { xhrAbort: false });
      this.fetchParams = { ...this.fetchParams };
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.columns.find(item => item.dataIndex === 'lossUser').dictItems = data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    async deleteBreakOrder() {
      console.log(this.selectList, 'deleteToolsOrder');
      let param = this.selectList[0].lossNo;
      let res = await deleteLossOrder({ orderNo: param });
      if (res.resultCode == 200) {
        this.$notify({
          title: '提示',
          message: '作废成功',
          type: 'success'
        });
      }
      this.$refs.table.DO_REFRESH();
    },
    editOrder(val) {
      this.orderNo = val;
      console.log('orderNo:::', this.orderNo);
      this.editLossOrderShow = true;
    },
    showOrder(val) {
      this.orderNo = val;
      console.log('orderNo:::', this.orderNo);
      this.lossOrderShow = true;
    },
    selectChange(val) {
      this.selectList = val;
    },
    // 点击搜索
    handleSearch(e) {
      console.log('e:::', e);
      this.fetchParams = Object.assign({ xhrAbort: false }, e);
      this.fetchParams = { ...this.fetchParams };
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    }
  }
};
</script>

<style scoped></style>
